package cn.itsource.user.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.user.constant.WechatConstant;
import cn.itsource.user.domain.LogininfoDto;
import cn.itsource.user.service.IWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/wechat")
public class WechatController {

    @Autowired
    private IWechatService service;

    @RequestMapping("/toLogin")
    public String erweima(){
        String replace = WechatConstant.CODE_URL.replace("APPID", WechatConstant.APPID)
                .replace("REDIRECT_URI", WechatConstant.REDIRECT_URI);
        return "redirect:" + replace;
    }

    @RequestMapping("/callback")
        public String huidiao(String code){
            return "redirect:http://localhost:8081/callback.html?code="+code;
    }

    @PostMapping("/handerlcallback")
    @ResponseBody
    public AjaxResult  handerlcallback(@RequestBody Map<String,String>  parem){
        try {
            Map<String, Object> rusltmap = service.handerlcallback(parem.get("code"));
            return  new AjaxResult().setResultObj(rusltmap);
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }
    }

    //绑定
    @PostMapping("/binder")
    @ResponseBody
    public AjaxResult binder(@RequestBody LogininfoDto logininfoDto){
        try {
            Map<String,Object> map = service.binder(logininfoDto);
            return new AjaxResult().setResultObj(map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }

    }
}
